package com.hxh.components
{
	import com.hxh.utils.Drawings;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.geom.ColorTransform;
	
	public class CustomColorPicker extends Sprite
	{
		public static const CONFIRM_COLOR:String = "CustomColorPicker001";
		public static const CANCEL_COLOR:String = "CustomColorPicker002";
		
		private const WIDTH:int = 150;
		private const HEIGHT:int = 80;
		
		private var _barBitmap:Bitmap;
		private var _spectrum:Sprite;
		private var _cursor:Sprite;
		private var _picker:Sprite;
		private var _pickerBorder:Sprite;
		private var _pickedColor:uint;
		
		public function CustomColorPicker(color:uint=0)
		{
			super();
			_pickedColor = color;
			//build the frame
			buildFrame();
			//Build the spectrum
			buildSpectrum();
			//Build the cursor
			buildCursor();
		}
				
		public function get PickedColor():uint
		{
			return _pickedColor;
		}

		public function set PickedColor(v:uint):void
		{
			_pickedColor = v;
		}
		
		public function destroy():void{
			if (_spectrum.visible){
				toggleSpectrum();
			}
			parent.removeChild(this);
		}

		private function buildFrame():void{
			//Square frame
			graphics.beginFill(0xE0E0E0, 1);
			graphics.drawRoundRect(0, 0, 60, 60, 10, 10);
			graphics.endFill();
			
			//Button OK
			var btnOK:Sprite = new CustomPickerYesIcon();
			addChild(btnOK);
			btnOK.x = 50;
			btnOK.y = 50;
			btnOK.alpha = 0.5;
			btnOK.scaleX = 0.8;
			btnOK.scaleY = 0.8;
			btnOK.filters = [new DropShadowFilter(1, 45, 0, 1, 2, 2, 2, BitmapFilterQuality.HIGH)];
			btnOK.addEventListener(MouseEvent.MOUSE_OVER, buttonMouseOver);
			btnOK.addEventListener(MouseEvent.MOUSE_OUT, buttonMouseOut);
			btnOK.addEventListener(MouseEvent.CLICK, confirmColor);
		
			//Button Cancel
			var btnCancel:Sprite = new CustomPickerNoIcon();
			addChild(btnCancel);
			btnCancel.x = 50;
			btnCancel.y = 10;
			btnCancel.alpha = 0.5;
			btnCancel.scaleX = 0.8;
			btnCancel.scaleY = 0.8;
			btnCancel.filters = [new DropShadowFilter(1, 45, 0, 1, 2, 2, 2, BitmapFilterQuality.HIGH)];
			btnCancel.addEventListener(MouseEvent.MOUSE_OVER, buttonMouseOver);
			btnCancel.addEventListener(MouseEvent.MOUSE_OUT, buttonMouseOut);
			btnCancel.addEventListener(MouseEvent.CLICK, cancelClick);
			
			//Picker
			_picker = new Sprite();
			addChild(_picker);
			_picker.graphics.beginFill(_pickedColor,1);
			_picker.graphics.drawRoundRect(3, 3, 36, 54, 5, 5);
			_picker.graphics.endFill();
			_picker.addEventListener(MouseEvent.MOUSE_OVER, pickerMouseOver);
			_picker.addEventListener(MouseEvent.MOUSE_OUT, pickerMouseOut);
			_picker.addEventListener(MouseEvent.CLICK, pickerClick);
			
			_pickerBorder = new Sprite();
			addChild(_pickerBorder);
			_pickerBorder.graphics.lineStyle(2, 0x0090FF, 1);
			_pickerBorder.graphics.drawRoundRect(3, 3, 36, 54, 5, 5);
			_pickerBorder.visible = false;
			
			//Effect
			filters = [new DropShadowFilter(4, 45, 0, 0.5, 6, 6, 1, BitmapFilterQuality.HIGH)];
		}
		
		private function buttonMouseOver(evt:MouseEvent):void{
			Sprite(evt.target).scaleX = 1.1;
			Sprite(evt.target).scaleY = 1.1;
		}
		
		private function buttonMouseOut(evt:MouseEvent):void{
			Sprite(evt.target).scaleX = 0.8;
			Sprite(evt.target).scaleY = 0.8;	
		}
		
		private function pickerMouseOver(evt:MouseEvent):void{
			_pickerBorder.visible = true;
		}
		
		private function pickerMouseOut(evt:MouseEvent):void{
			_pickerBorder.visible = false;	
		}
		
		private function pickerClick(evt:MouseEvent):void{
			//Bring the color picker to front
			parent.addChild(this);
			toggleSpectrum();
		}
		
		private function cancelClick(evt:MouseEvent):void{
			destroy();
			dispatchEvent(new Event(CANCEL_COLOR));
		}
		
		private function confirmColor(evt:MouseEvent):void{
			dispatchEvent(new Event(CONFIRM_COLOR));
		}
		
		private function browseColor(evt:MouseEvent):void{
			_cursor.x = evt.localX+65;
			_cursor.y = evt.localY-23;
		}
		
		private function pickColor(evt:MouseEvent):void{
			
			var pickedColor:uint = _barBitmap.bitmapData.getPixel(evt.localX, evt.localY);
			_pickedColor = pickedColor;
			var colorTransform:ColorTransform = new ColorTransform();
			colorTransform.color= pickedColor;
			_picker.transform.colorTransform=colorTransform;
			toggleSpectrum();
		}
		
		
		private function buildSpectrum():void{
			//Load the color bar into a BitmapData
			var bitmapData:BitmapData = new BitmapData(WIDTH, HEIGHT, true, 0);
			bitmapData.draw(Drawings.ColorSpectrumBar(WIDTH ,HEIGHT));
			//Load the BitmapData to a Bitmap
			_barBitmap = new Bitmap(bitmapData);
			//Create spectrum sprite
			_spectrum = new Sprite();
			addChild(_spectrum);
			//Draw the frame
			_spectrum.graphics.beginFill(0xE0E0E0, 1);
			_spectrum.graphics.drawRoundRect(-3, -3, WIDTH+6, HEIGHT+6, 10, 10);
			_spectrum.graphics.endFill();
			//Position the frame
			_spectrum.y = -23;
			_spectrum.x = 65;
			
			//The color bar holder
			var holder:Sprite = new Sprite();
			_spectrum.addChild(holder);
			//Add the color bar to the holder
			holder.addChild(_barBitmap);
			//Event handlers
			holder.addEventListener(MouseEvent.MOUSE_MOVE, browseColor);
			holder.addEventListener(MouseEvent.CLICK, pickColor);
			//Effect
			_spectrum.visible = false;	
		}
		
		private function buildCursor():void{
			_cursor = new Sprite();
			addChild(_cursor);
			_cursor.graphics.lineStyle(1, 0xFFFFFF, 1);
			_cursor.graphics.drawRect(-2,-2,4,4);
			_cursor.visible = false;
		}
		
		private function toggleSpectrum():void{
			_spectrum.visible = !_spectrum.visible;
			_cursor.visible = !_cursor.visible;
		}
	}
}